package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.PEvaluate;
import com.ruoyi.system.domain.bo.PEvaluateBo;
import com.ruoyi.system.domain.vo.PEvaluateVo;
import com.ruoyi.system.mapper.PEvaluateMapper;
import com.ruoyi.system.service.IPEvaluateService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 用户评价咨询师Service业务层处理
 *
 * @author ruoyi
 * @date 2022-07-22
 */
@RequiredArgsConstructor
@Service
public class PEvaluateServiceImpl implements IPEvaluateService {

    private final PEvaluateMapper baseMapper;

    /**
     * 查询用户评价咨询师
     */
    @Override
    public PEvaluate queryById(Long id) {
        return baseMapper.selectByLeftId(id);
    }

    /**
     * 查询用户评价咨询师列表
     */
    @Override
    public TableDataInfo<PEvaluate> queryPageList(PEvaluate bo, PageQuery pageQuery) {
        QueryWrapper<PEvaluate> lqw = Wrappers.query();
        lqw.eq("e.del_flag", UserConstants.USER_NORMAL);
        lqw.like(StringUtils.isNotBlank(bo.getUsername()), "u.user_name", bo.getUsername());
        lqw.like(StringUtils.isNotBlank(bo.getAdviserName()), "d.user_name", bo.getAdviserName());
        lqw.like(StringUtils.isNotBlank(bo.getEvaluateTitle()), "e.evaluate_title", bo.getEvaluateTitle());
        lqw.eq(ObjectUtil.isNotEmpty(bo.getAdviserId()), "e.adviser_id", bo.getAdviserId());
        Page<PEvaluate> result = baseMapper.selectLeftUserPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户评价咨询师列表
     */
    @Override
    public List<PEvaluateVo> queryList(PEvaluateBo bo) {
        LambdaQueryWrapper<PEvaluate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PEvaluate> buildQueryWrapper(PEvaluateBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PEvaluate> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, PEvaluate::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getEvaluateMsg()), PEvaluate::getEvaluateMsg, bo.getEvaluateMsg());
        lqw.eq(StringUtils.isNotBlank(bo.getEvaluateTag()), PEvaluate::getEvaluateTag, bo.getEvaluateTag());
        lqw.eq(bo.getAdviserId() != null, PEvaluate::getAdviserId, bo.getAdviserId());
        lqw.eq(bo.getScore() != null, PEvaluate::getScore, bo.getScore());
        lqw.eq(StringUtils.isNotBlank(bo.getEvaluateType()), PEvaluate::getEvaluateType, bo.getEvaluateType());
        lqw.eq(StringUtils.isNotBlank(bo.getEvaluateTitle()), PEvaluate::getEvaluateTitle, bo.getEvaluateTitle());
        return lqw;
    }

    /**
     * 新增用户评价咨询师
     */
    @Override
    public Boolean insertByBo(PEvaluateBo bo) {
        PEvaluate add = BeanUtil.toBean(bo, PEvaluate.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户评价咨询师
     */
    @Override
    public Boolean updateByBo(PEvaluateBo bo) {
        PEvaluate update = BeanUtil.toBean(bo, PEvaluate.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PEvaluate entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户评价咨询师
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<PEvaluate> queryByAdviserId(Long adviserId, String type) {
        return baseMapper.queryByAdviserId(adviserId, type);
    }

    @Override
    public void insertFontByBo(PEvaluateBo bo) {
        PEvaluate add = BeanUtil.toBean(bo, PEvaluate.class);
        add.setUserId(LoginHelper.getUserId());
        baseMapper.insert(add);
    }

    @Override
    public TableDataInfo<PEvaluate> evaluateList(Long adviserId, String type, PageQuery pageQuery) {
        pageQuery.setPageNum((pageQuery.getPageNum() - 1) * pageQuery.getPageSize());
        return TableDataInfo.build(this.baseMapper.evaluateList(pageQuery, type, adviserId));
    }
}
